package com.saifan.wyy_ov.utils;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;

import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.data.bean.PopBean;
import com.saifan.wyy_ov.model.interfaces.IListViewOnClick;

/**
 * @author WangLi
 * @version V1.0
 * @date 2015年3月18日 下午11:00:46
 * @Description Actionbar右边显示操作栏
 */

public class PopUtil {
    @SuppressWarnings("deprecation")
    @SuppressLint({"InflateParams", "RtlHardcoded"})
    public static void showPop(Context context, List<PopBean> list, final IListViewOnClick onClick, int weight, int height) {
        View contentView = ((Activity) context).getLayoutInflater().inflate(R.layout.popupwindow_layout, null);
        ListView listView = (ListView) contentView.findViewById(R.id.pop_list);
        final PopupWindow popupWindow = new PopupWindow(contentView, DensityUtil.dip2px(context, weight), DensityUtil.dip2px(context, height), false);
        CommonAdapter<PopBean> dapter = new CommonAdapter<PopBean>(context, list, R.layout.pop_item) {

            @Override
            public void convert(ViewHolderHelper helper, PopBean item) {
                helper.setText(R.id.pop_text, item.getName());
                helper.setImageResource(R.id.pop_img, item.getDrawable());
            }
        };
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                onClick.onClick(arg2);
                popupWindow.dismiss();
            }
        });
        listView.setAdapter(dapter);
        // 需要设置一下此参数，点击外边可消失
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        // 设置点击窗口外边窗口消失
        popupWindow.setOutsideTouchable(true);
        // 设置此参数获得焦点，否则无法点击
        popupWindow.setFocusable(true);
        // 状态栏的高度
        Rect frame = new Rect();
        ((Activity) context).getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        popupWindow.showAtLocation(contentView, Gravity.RIGHT | Gravity.TOP, 0, ((AppCompatActivity) context).getSupportActionBar()
                .getHeight() + statusBarHeight);
    }

    @SuppressWarnings("deprecation")
    @SuppressLint({"InflateParams", "RtlHardcoded"})
    public static void showPop(Context context, View view, List<PopBean> list, final IListViewOnClick onClick) {
        View contentView = ((Activity) context).getLayoutInflater().inflate(R.layout.popupwindow_layout, null);
        ListView listView = (ListView) contentView.findViewById(R.id.pop_list);
        final PopupWindow popupWindow = new PopupWindow(contentView, DensityUtil.dip2px(context, 160), DensityUtil.dip2px(context, 207), false);
        CommonAdapter<PopBean> dapter = new CommonAdapter<PopBean>(context, list, R.layout.pop_item) {

            @Override
            public void convert(ViewHolderHelper helper, PopBean item) {
                helper.setText(R.id.pop_text, item.getName());
                helper.setImageResource(R.id.pop_img, item.getDrawable());
            }
        };
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                onClick.onClick(arg2);
                popupWindow.dismiss();
            }
        });
        listView.setAdapter(dapter);
        // 需要设置一下此参数，点击外边可消失
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        // 设置点击窗口外边窗口消失
        popupWindow.setOutsideTouchable(true);
        // 设置此参数获得焦点，否则无法点击
        popupWindow.setFocusable(true);
        // 状态栏的高度

        popupWindow.showAsDropDown(view);
    }

    @SuppressWarnings("deprecation")
    @SuppressLint({"InflateParams", "RtlHardcoded"})
    public static void showPop(Context context, List<PopBean> list, final IListViewOnClick onClick) {
        View contentView = ((Activity) context).getLayoutInflater().inflate(R.layout.popupwindow_layout, null);
        ListView listView = (ListView) contentView.findViewById(R.id.pop_list);
        final PopupWindow popupWindow = new PopupWindow(contentView, DensityUtil.dip2px(context, 160), DensityUtil.dip2px(context, 207), false);
        CommonAdapter<PopBean> dapter = new CommonAdapter<PopBean>(context, list, R.layout.pop_item) {

            @Override
            public void convert(ViewHolderHelper helper, PopBean item) {
                helper.setText(R.id.pop_text, item.getName());
                helper.setImageResource(R.id.pop_img, item.getDrawable());
            }
        };
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                onClick.onClick(arg2);
                popupWindow.dismiss();
            }
        });
        listView.setAdapter(dapter);
        // 需要设置一下此参数，点击外边可消失
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        // 设置点击窗口外边窗口消失
        popupWindow.setOutsideTouchable(true);
        // 设置此参数获得焦点，否则无法点击
        popupWindow.setFocusable(true);
        // 状态栏的高度
        Rect frame = new Rect();
        ((Activity) context).getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        popupWindow.showAtLocation(contentView, Gravity.RIGHT | Gravity.TOP, 0, ((AppCompatActivity) context).getSupportActionBar()
                .getHeight() + statusBarHeight);
    }


    @SuppressWarnings("deprecation")
    @SuppressLint({"InflateParams", "RtlHardcoded"})
    public static PopupWindow createPopupWindow(Context context,   List<PopBean> list, final IListViewOnClick onClick) {
        View view = ((Activity) context).getLayoutInflater().inflate(R.layout.pop_layout, null);
        ListView listView = (ListView) view.findViewById(R.id.listView);
        final PopupWindow popupWindow = new PopupWindow();
        CommonAdapter adapter = new CommonAdapter<PopBean>(context, list, R.layout.list_menu) {
            @Override
            public void convert(ViewHolderHelper helper, PopBean item) {
                helper.setText(R.id.list_item, item.getName());

            }
        };
        listView.setAdapter(adapter);

        // 需要设置一下此参数，点击外边可消失
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        // 设置点击窗口外边窗口消失
        popupWindow.setOutsideTouchable(true);
        // 设置此参数获得焦点，否则无法点击
        popupWindow.setFocusable(true);
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                onClick.onClick(arg2);
                popupWindow.dismiss();
            }
        });
        popupWindow.setContentView(view);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        popupWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        CommonAdapter<PopBean> dapter = new CommonAdapter<PopBean>(context, list, R.layout.list_menu) {

            @Override
            public void convert(ViewHolderHelper helper, PopBean item) {
                helper.setText(R.id.list_item, item.getName());

            }
        };

        listView.setAdapter(dapter);
        return popupWindow;

    }
}
